<?php
/**
 * 菜单管理
 * @author sundongdong
 * @date   2016-07-05
 */
class System_MenuController extends Controller_NeedLogin {
	/**
	 * 初始化
	 */
	public function init() {
		parent::init();
		$this->menuModel = new System_Model_Menu();
	}
	/**
	 * @desc 菜单列表
	 */
	public function indexAction () {
		// 获取菜单
		$menus = $this->menuModel->getMenu(0);
		$menuLevel = array();
		foreach ($menus as $menuKey => $menuValue) {
			if($menuValue['fid'] == 0){
				$menuValue['children'] = array();
				$menuLevel[$menuKey] = $menuValue;
				foreach ($menus as $secondMenuKey => $secondMenuValue) {
					if($secondMenuValue['fid'] == $menuValue['menu_id']){
						$secondMenuValue['children'] = array();
						$menuLevel[$menuKey]['children'][$secondMenuKey] = $secondMenuValue;
						foreach ($menus as $thirdMenuKey => $thirdMenuValue) {
							if($thirdMenuValue['fid'] == $secondMenuValue['menu_id']){
								$menuLevel[$menuKey]['children'][$secondMenuKey]['children'][$thirdMenuKey] = $thirdMenuValue;
							}
						}
					}
				}
			}
		}
		
		$this->smarty->assign('menus', $menus);
    	$this->smarty->assign('datas', $menuLevel);
        $this->smarty->display('sys_menu.tpl');
	}
	/**
	 * 添加菜单
	 */
	public function replacemenuAction() {
		// 获取post数据
		$getData = $this->_request->getPost();
		$data = array();
    	$data['fid'] = $getData['menu_id'];
    	$data['name'] = $getData['name'];
    	$data['tips'] = $getData['description'];
    	$data['module'] = $getData['module'];
    	$data['controller'] = $getData['controller'];
    	$data['action'] = $getData['action'];
    	$data['menu_id'] =  $getData['menu_id_edit'];
    	$data['is_show'] = $getData['is_show'];
    	$data['create_user'] = $this->_session->user_name;
    	
    	$res = $this->menuModel->replaceMenu($data);
    	echo json_encode($res);
	}
	/**
	 * 获取菜单信息
	 */
	public function getmenuAction() {
		$menu_id = $this->getRequest()->getParam('id');
        $menus = $this->menuModel->getMenuById($menu_id);
        echo json_encode($menus);
	}
	/**
	 * 获取元素
	 */
	public function getnoteAction() {
		$menusHtml = '';
    	$menu_id = $this->getRequest()->getParam('id');
    	$menus = $this->menuModel->getNoteByPid($menu_id);
    	if($menus){
            $menusHtml = '';
    		foreach($menus as $menu)
    		{
    			$menu_url = $menu['module']?'/'.$menu['module']:'';
    			$menu_url .= $menu['controller']?'/'.$menu['controller']:'';
    			$menu_url .= $menu['action']?'/'.$menu['action']:'';
    			
    			$menusHtml .= '<tr>';
    			$menusHtml .= "<td width='100' height='50' align='center'>{$menu['menu_id']}</td>";
    			$menusHtml .= "<td width='150' align='center'>{$menu['name']}</td>";
    			$menusHtml .= "<td width='100' align='center'>{$menu_url}</td>";
    			$menusHtml .= "<td width='150' align='center'>{$menu['tips']}</td>";
    			$menusHtml .= "<td width='100' align='center'><a class='btn btn-danger btn-sm' menu_id='{$menu['menu_id']}' menu_name='{$menu['name']}' name='del_menu' data-original-title='删除' data-toggle='tooltip' href='javascript:void(0);'><i class='fa fa-remove'></i></a></td>";
    			$menusHtml .= '</tr>';
    		}
    	}
    	echo $menusHtml;
	}
	/**
	 * 删除菜单
	 */
	public function delmenuAction() {
		$menu_id = $this->getRequest()->getParam('menu_id');
    	$menu_name = $this->getRequest()->getParam('menu_name');
    	$status = $this->menuModel->delMenu($menu_id);
    	echo json_encode($status);
	}
}